package com.wrupple.muba.catalogs.client.activity.process.state;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.shared.EventBus;
import com.wrupple.muba.catalogs.client.activity.process.CatalogImportActivityProcess.ImportData;
import com.wrupple.muba.catalogs.client.activity.process.impl.ImportDataHandler;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.shared.StateTransition;

public class SimpleImportDataHandler implements ImportDataHandler {

	class CreateNext extends DataCallback<JavaScriptObject> {
		List<JavaScriptObject> entries;

		int index;

		StorageManager storage;

		String type;

		public CreateNext(List<JavaScriptObject> entries, int index,
				com.wrupple.muba.common.shared.State.StorageManager storage,
				String type) {
			super();
			this.entries = entries;
			this.index = index;
			this.storage = storage;
			this.type = type;
		}

		@Override
		public void execute() {
			if (result == null) {
				GWT.log("entry NOT created, retrying");
			} else {
				index++;
				CatalogEntry entry = result.cast();
				results.add(entry);
				GWT.log("entry created :");
			}
			if (index < entries.size()) {
				JavaScriptObject imported = entries.get(index);
				JavaScriptObject create = generateMainCatalogObjectToPersist(imported);
				StateTransition<JavaScriptObject> creationCallback = new CreateNext(
						entries, index, storage, type);
				StateTransition<JavaScriptObject> trigger = generateObjectImportTrigger(imported);
				if (trigger != null) {
					creationCallback.hook(trigger);
				}
				storage.create(type, create, creationCallback);
			} else {
				if(finalHook!=null){
					callback.hook(finalHook);
				}
				callback.setResultAndFinish(results);
				GWT.log("Done");
			}

		}
	}

	protected StorageManager storage;
	private List<CatalogEntry> results;
	private StateTransition<List<CatalogEntry>> callback;
	protected StateTransition<List<CatalogEntry>> finalHook;
	protected String[] columnNames;

	@Override
	public void start(ImportData parameter,
			StateTransition<List<CatalogEntry>> onDone, EventBus bus) {
		results = new ArrayList<CatalogEntry>();
		this.columnNames = parameter.columnNames;
		this.callback = onDone;
		new CreateNext(parameter.csv, 0, storage, parameter.catalog).execute();
	}

	protected StateTransition<JavaScriptObject> generateObjectImportTrigger(
			JavaScriptObject imported) {
		// default implementation assumes theres no other objects to create
		// ofter the persistance manager has assigned an id to the imported object
		return null;
	}

	protected JavaScriptObject generateMainCatalogObjectToPersist(
			JavaScriptObject imported) {
		// default implementation assumes the imported data exactly matches the
		// persisted data
		return imported;
	}

	@Override
	public void setContext(com.wrupple.muba.common.shared.State.Context context) {
		this.storage = context.getStorageManager();
	}

}